<%@ page language="java" contentType="text/html; charset=windows-1255" pageEncoding="windows-1255"%>
<%@ include file="admin_Header.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Manage Supermarket</title>
</head>
<body>

<script type="text/javascript">
 	function openPage(pageURL)
 	{
		alert(pageURL);
 		window.location.href = pageURL;
 	}
</script>

<center>
	<h1>Product Compare Application</h1>
	<button id="List" onclick="window.location.href='admin_ListProducts.jsp'">Get Product List</button>
	<button id="AddProduct" onclick="window.location.href='admin_AddProduct.jsp'">Add New Product</button>
	<button id="AddSuperMarket" onclick="window.location.href='admin_AddSupermarket.jsp'">Add New SuperMarket</button>
	<button id="ManageSuperMarket" onclick="window.location.href='admin_ManageSupermarket.jsp'">Manage Supermarket</button>
	<button id="DeleteProduct" onclick="window.location.href='admin_DeleteProduct.jsp'">Delete Product</button>
	<button id="DeleteSupermarket" onclick="window.location.href='admin_DeleteSupermarket.jsp'">Delete Supermarket</button>
</center>
<center>
<br><br>
<%
	// Update Product Price
	if ((request.getParameter("newPrice") != null) &&
	    (request.getParameter("product") != null) &&
	    (request.getParameter("supermarket") != null))
	{
		try
		{
			Product product = iproducts.FindProductById(Integer.valueOf(request.getParameter("product")));
			Supermarket supermarket = iproducts.FindSupermarketById(Integer.valueOf(request.getParameter("supermarket")));
			Double price = Double.parseDouble(request.getParameter("newPrice"));
			iproducts.AddProductToSupermarket(supermarket, product, price);
			ProductSupermarket verifyProductSupermarket = iproducts.FindProductSupermarket(product, supermarket);
			if ((verifyProductSupermarket.getProduct().getProductId().equals(product.getProductId())) &&
			    (verifyProductSupermarket.getProduct().getProductName().equals(product.getProductName())) &&
			    (verifyProductSupermarket.getProduct().getProductUnit().equals(product.getProductUnit())) &&
			    (verifyProductSupermarket.getProduct().getProductQuantityPerUnit().equals(product.getProductQuantityPerUnit())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketId().equals(supermarket.getSupermarketId())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketName().equals(supermarket.getSupermarketName())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketLocationLatitude().equals(supermarket.getSupermarketLocationLatitude())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketLocationLongitude().equals(supermarket.getSupermarketLocationLongitude())) &&
				(verifyProductSupermarket.getPrice().equals(price)))
				{
				    out.println("\nProduct added to Supermarket successfully");
				}
			else
			{
				out.println("\nSomething went wrong, please check");
			}
		}
		catch (NumberFormatException e)
		{
		    throw new Exception("Parameter input error");
		}
		catch (Exception e)
	    {
	        throw new Exception("Error in connecting to database, please contact your administrator");
		}
	}
	
	// Add/Update Product Price
	if ((request.getParameter("supermarket") != null) &&
	    (request.getParameter("product") != null) &&
	    (request.getParameter("priceToAddUpdate") != null)) 
	{
		try
		{
		    Supermarket supermarket = iproducts.FindSupermarketById(Integer.valueOf(request.getParameter("supermarket")));
			Product product = iproducts.FindProductById(Integer.valueOf(request.getParameter("product")));
			Double price = Double.valueOf(request.getParameter("priceToAddUpdate"));
			
			iproducts.AddProductToSupermarket(supermarket, product, price);
			ProductSupermarket verifyProductSupermarket = iproducts.FindProductSupermarket(product, supermarket);
			if ((verifyProductSupermarket.getProduct().getProductId().equals(product.getProductId())) &&
			    (verifyProductSupermarket.getProduct().getProductName().equals(product.getProductName())) &&
			    (verifyProductSupermarket.getProduct().getProductUnit().equals(product.getProductUnit())) &&
			    (verifyProductSupermarket.getProduct().getProductQuantityPerUnit().equals(product.getProductQuantityPerUnit())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketId().equals(supermarket.getSupermarketId())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketName().equals(supermarket.getSupermarketName())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketBranch().equals(supermarket.getSupermarketBranch())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketLocationLatitude().equals(supermarket.getSupermarketLocationLatitude())) &&
				(verifyProductSupermarket.getSupermarket().getSupermarketLocationLongitude().equals(supermarket.getSupermarketLocationLongitude())) &&
				(verifyProductSupermarket.getPrice().equals(price)))
				{
				    out.println("\nProduct added to Supermarket successfully");
				}
			else
			{
				out.println("\nSomething went wrong, please check");
			}
			response.sendRedirect("admin_ManageSupermarket.jsp?supermarketId=" + request.getParameter("supermarket"));
		}
		catch (NumberFormatException e)
		{
		    throw new Exception("Parameter input error");
		}
		catch (Exception e)
		{
		    throw new Exception("Error in connecting to database, please contact your administrator");
		}
	}
	
	if (request.getParameter("supermarketId") != null)
	{
		try
		{
			Integer supermarketId = Integer.valueOf(request.getParameter("supermarketId"));
			Supermarket supermarket = iproducts.FindSupermarketById(supermarketId);
			Set<ProductSupermarket> productSupermarkets = iproducts.FindAllProductSupermarkets(supermarket);
			
			out.println("<table border=\"1\">");
			out.println("<tr>");
			out.println("<th colspan=\"5\">" + supermarket.getSupermarketName() + "</th>");	
			out.println("</tr>");
			out.println("<tr>");
			out.println("<tr bgcolor=\"grey\"><th>Product</th><th>Product Unit</th><th>Unit Quantity</th><th colspan=\"2\">Price</th></tr>");
			out.println("</tr>");
			for (Iterator<ProductSupermarket> iterPS = productSupermarkets.iterator(); iterPS.hasNext();)
			{
				ProductSupermarket productSupermarket = (ProductSupermarket) iterPS.next();
				out.println("<tr><td>" + productSupermarket.getProduct().getProductName() + "</td>");
				out.println("<td>" + productSupermarket.getProduct().getProductUnit() + "</td>");
				out.println("<td>" + productSupermarket.getProduct().getProductQuantityPerUnit() + "</td>" );
				out.println("<td><form action=\"admin_ManageSupermarket.jsp\"><input type=\"text\" name=\"newPrice\" value=\""+ productSupermarket.getPrice() + "\"></input></td>" );
				out.println("<input type=\"hidden\" name=\"product\" value=\""+ productSupermarket.getProduct().getProductId() + "\"></input>");
				out.println("<input type=\"hidden\" name=\"supermarket\" value=\""+ productSupermarket.getSupermarket().getSupermarketId() + "\"></input>");
				out.println("<td><button type=\"submit\">Update Price</button>");
				out.println("</form></tr>");
			}	
			out.println("</table>");
			out.println("<br><br><br><br>");
			out.println("<table border=\"1\">");
			out.println("<tr>");
			out.println("<th colspan=\"2\">Add/Update Product</th>");
			out.println("</tr>");
			out.println("<tr>");
			out.println("<th>Product</th><th colspan=\"2\">Price</th>");
			out.println("</tr>");
			List<Product> products = iproducts.FindAllProducts();
			out.println("<form action=\"admin_ManageSupermarket.jsp\">");
			out.println("<input  name=\"supermarket\" type=\"hidden\"  value=\"" + supermarket.getSupermarketId() + "\">");
			out.println("<tr>");
			out.println("<td>");
			out.println("<select name=\"product\">");
			out.println("<option selected value=\"\">Select Product</option>");
			for (Product product : products)
			{
				out.println("<option value=\"" + product.getProductId() + "\"> " + product.getProductName() + " , "+ product.getProductQuantityPerUnit() + product.getProductUnit() + "</option>");	
			}
			out.println("</select>");
			out.println("</td>");
			out.println("<td>");
			out.println("<input name=\"priceToAddUpdate\" type=\"text\">");
			
			out.println("<button name=\"Submit\" type=\"submit\">Add/Update Product");
			out.println("</td>");
			out.println("</tr>");
			out.println("</form>");
			out.println("</table>");
		}
		catch (NumberFormatException e)
		{
		    throw new Exception("Parameter input error");
		}
		catch (Exception e)
		{
		    throw new Exception("Error in connecting to database, please contact your administrator");	
		}
	}
	else
	{
		out.println("<form action=\"admin_ManageSupermarket.jsp\">");
		try
		{
			List<Supermarket> supermarkets = iproducts.FindAllSupermarkets();
			out.println("<select name=\"supermarketId\">");
			for (Supermarket supermarket : supermarkets)
			{
				out.println("<option name=\"supermarketId\" value=\"" + supermarket.getSupermarketId() +"\"> " + supermarket.getSupermarketName() +"</option>");
			}
			out.println("</select>");
			out.println("<button type=\"submit\">Select</button>");
			out.println("</form>");
		}
		catch (Exception e)
		{
		    throw new Exception("Error in connecting to database, please contact your administrator");
		}
	}
%>
</center>
</body>
</html>